var template_pagin_model = function(){};
$.extend(template_pagin_model.prototype,
{
	total : 0,  /*总数*/
	size : 0,  /*每页多少条*/
	number : 0,  /*第几页*/
	totalpage:0,/*总页数*/
	recno1:0,/*正显示记录数的开始序号*/
	recno2:0,/*正显示记录数的结束序号*/
	pagin_obj:null,
	handler:null
});
$.fn.template_pagination=function(options){
	return this.each(function(){
		var pagin_model;
		var pagin=$.data(this,"pagination");
		if(pagin){
			pagin_model=$.extend(pagin.options,options);
		}else{
			pagin_model=$.extend({},$.fn.template_pagination.defaults,options);
			$.data(this,"pagination",{options:options});
		}
		pagin_model.init(this);
		pagin_model.displaypagin();
	});
}
$.fn.template_pagination.defaults={
		data:eval({}), /*数据*/
		pagin_info_total:"现显示第&nbsp;{recno1}&nbsp;-&nbsp;{recno2}&nbsp;条记录，共&nbsp;{reccount}&nbsp;条记录",
		pagin_info_size: "每页&nbsp;{size}&nbsp;条",
		init:function(pagin_obj)
		{
			var t_p_model = new template_pagin_model();
			t_p_model.total = this.data.total;
			t_p_model.size = this.data.size;
			t_p_model.number = this.data.number;
			t_p_model.totalpage = parseInt(t_p_model.total/t_p_model.size); 
			if (t_p_model.size*t_p_model.totalpage < t_p_model.total)
				t_p_model.totalpage = t_p_model.totalpage + 1;
			if (t_p_model.number > t_p_model.totalpage)
				t_p_model.number = t_p_model.totalpage;
			t_p_model.recno1 = (t_p_model.number-1)*t_p_model.size+1;
			if (t_p_model.recno1 < 0)
				t_p_model.recno1 = 0;
			if (t_p_model.number < t_p_model.totalpage)
				t_p_model.recno2 = t_p_model.number*t_p_model.size;
			else
				t_p_model.recno2 = t_p_model.total;
			t_p_model.pagin_obj = pagin_obj;
			t_p_model.handler = this.data.handler;
			$.data(this,"template_pagin_model",t_p_model);
		},
		showPage:function(page)
		{
			var t_p_model = $.data(this,"template_pagin_model");
			if (page < 1)
				page = 1;
			if (page > t_p_model.totalpage)
				page = t_p_model.totalpage;
			eval(t_p_model.handler+'('+page+')');
			/*var t_p_model = $.data(this,"template_pagin_model");
			t_p_model.number = page;
			if (t_p_model.number > t_p_model.totalpage)
				t_p_model.number = t_p_model.totalpage;
			t_p_model.recno1 = (t_p_model.number-1)*t_p_model.size+1;
			if (t_p_model.number < t_p_model.totalpage)
				t_p_model.recno2 = t_p_model.number*t_p_model.size;
			else
				t_p_model.recno2 = t_p_model.total;
			$.data(this,"template_pagin_model",t_p_model);
			this.displaypagin();*/
		},
		displaypagin:function()
		{
			var t_p_model = $.data(this,"template_pagin_model");
			$(t_p_model.pagin_obj).find('input[type=text].pagination-num:first').attr("value",t_p_model.number);
			$(t_p_model.pagin_obj).find('span.pagination-totalpage:first').html(t_p_model.totalpage);
			$(t_p_model.pagin_obj).find('div.pagination-info:first').html(
					this.pagin_info_total.replace('{reccount}',t_p_model.total).replace('{recno1}',t_p_model.recno1).replace('{recno2}',t_p_model.recno2)
				);
			$(t_p_model.pagin_obj).find('span.pagination-info-size:first').html(
				this.pagin_info_size.replace('{size}',t_p_model.size)
				);
			var t_p_obj = this;
			if (t_p_model.number <= 1)
			{
				$(t_p_model.pagin_obj).find('span.pagination-first').parent().addClass("pagination-icon-disabled");
				$(t_p_model.pagin_obj).find('span.pagination-prev').parent().addClass("pagination-icon-disabled");
				$(t_p_model.pagin_obj).find('span.pagination-first').parent().unbind(".pagination");
				$(t_p_model.pagin_obj).find('span.pagination-prev').parent().unbind(".pagination");
			}
			else
			{
				$(t_p_model.pagin_obj).find('span.pagination-first').parent().removeClass("pagination-icon-disabled");
				$(t_p_model.pagin_obj).find('span.pagination-prev').parent().removeClass("pagination-icon-disabled");
				$(t_p_model.pagin_obj).find('span.pagination-first').parent().unbind(".pagination").bind("click.pagination",function(){
					t_p_obj.showPage(1);
				});
				$(t_p_model.pagin_obj).find('span.pagination-prev').parent().unbind(".pagination").bind("click.pagination",function(){
					t_p_obj.showPage(t_p_model.number-1);
				});
			}
			if (t_p_model.number >= t_p_model.totalpage)
			{
				$(t_p_model.pagin_obj).find('span.pagination-next').parent().addClass("pagination-icon-disabled");
				$(t_p_model.pagin_obj).find('span.pagination-last').parent().addClass("pagination-icon-disabled");
				$(t_p_model.pagin_obj).find('span.pagination-next').parent().unbind(".pagination");
				$(t_p_model.pagin_obj).find('span.pagination-last').parent().unbind(".pagination");
			}
			else
			{
				$(t_p_model.pagin_obj).find('span.pagination-next').parent().removeClass("pagination-icon-disabled");
				$(t_p_model.pagin_obj).find('span.pagination-last').parent().removeClass("pagination-icon-disabled");
				$(t_p_model.pagin_obj).find('span.pagination-next').parent().unbind(".pagination").bind("click.pagination",function(){
					t_p_obj.showPage(t_p_model.number+1);
				});
				$(t_p_model.pagin_obj).find('span.pagination-last').parent().unbind(".pagination").bind("click.pagination",function(){
					t_p_obj.showPage(t_p_model.totalpage);
				});
			}
			$(t_p_model.pagin_obj).find('input[type=text].pagination-num:first').unbind(".pagination").bind("keydown.pagination",function(e){
				if(e.keyCode==13){
				var p = parseInt($(this).val())||1;
				t_p_obj.showPage(p);
				}
			});
		}
	};
